//
//  CLEmoiconKeyBoard.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/19.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit

//重用标识符
private let CLEmoiconKeyBoardCell = "cell"
class CLEmoiconKeyBoard: UIView {

    // MARK:  懒加载控件
    // 表情键盘的toolbar
    private lazy var toolBar:CLEmoiconToolBar = {
        let view = CLEmoiconToolBar(frame: CGRectZero)
        return view
    }()

    //pagecontrol
    private lazy var pageControl :UIPageControl = {
    
        let page = UIPageControl()
        //选中的颜色
        page.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        //为选中的颜色
        page.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        page.numberOfPages = 6
        return page
    }()
    //表情视图
    private lazy var emoiconCollectionView:UICollectionView = {
    
        let flowLayout = UICollectionViewFlowLayout()
        //视图滚动方向
        flowLayout.scrollDirection = .Horizontal
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.backgroundColor = self.backgroundColor
        view.bounces = false
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        //分页效果
        view.pagingEnabled = true
        //数据源
        view.dataSource = self
        view.delegate = self
        //注册
        view.registerClass(CLEmoiconCollectionViewCell.self , forCellWithReuseIdentifier: CLEmoiconKeyBoardCell)
        return view
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUPUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUPUI(){
        
      
        
        //设置背景颜色
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        //添加控件  
        addSubview(emoiconCollectionView)
        addSubview(toolBar)
        addSubview(pageControl)
        //设置约束
        emoiconCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoiconCollectionView)
            make.height.equalTo(10)
        }
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(30)
        }
        
        
        //设置闭包
        toolBar.selectedButtonClosure = { [weak self] (type :CLEmoiconToolBarButtonType) in
            let indexPath:NSIndexPath
            switch type {
            case .Default :
                print("默认")
                indexPath = NSIndexPath(forRow: 0, inSection: 0)
            case.Emoji:
                print("emoji")
                indexPath = NSIndexPath(forRow: 0, inSection: 1)
            case.Lxh:
                print("浪小花")
                indexPath = NSIndexPath(forRow: 0, inSection: 2)
            }
            self!.emoiconCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            //绑定pagecontrol数据
            self?.setPageControlDataForIndexPath(indexPath)
        }
    }
    //通过indexPath设置pagecontrol的数据
    private func setPageControlDataForIndexPath(indexPath:NSIndexPath){
        
        pageControl.numberOfPages = CLEmoiconTools.sharedTools.allEmoiconArray[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
    //设置Frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //获取表情视图的布局方式
        let flowLayout = emoiconCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        //设置大小
        flowLayout.itemSize = emoiconCollectionView.size
        //设置水平间距
        flowLayout.minimumInteritemSpacing = 0
        //设置垂直间距
        flowLayout.minimumLineSpacing = 0
    }
    
    
}

//数据源方法
extension CLEmoiconKeyBoard:UICollectionViewDataSource,UICollectionViewDelegate {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return CLEmoiconTools.sharedTools.allEmoiconArray.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return CLEmoiconTools.sharedTools.allEmoiconArray[section].count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CLEmoiconKeyBoardCell, forIndexPath: indexPath) as! CLEmoiconCollectionViewCell
        cell.emoiconModel = CLEmoiconTools.sharedTools.allEmoiconArray[indexPath.section][indexPath.item]
        return cell
    }
//scrollView代理方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //获取当前‘emoiconCollectionView’显示的cell
        let cells = emoiconCollectionView.visibleCells()

        //如果当前显示的是两个cell
        if cells.count == 2 {
            //定义常量 可以赋值一次
            let section:Int
            //取出两个cell 对比两个cell谁在屏幕上显示的范围多
            let firstCell = cells.first!
            let secondCell = cells.last!
            //两个cell的x  减去滚动距离   绝对值越小,显示的范围越多
            let firsrR = abs(Int32(firstCell.x - scrollView.contentOffset.x))
            let secondR = abs(Int32(secondCell.x - scrollView.contentOffset.x))
            let cell:UICollectionViewCell
            
            //值越小显示的范围越大
            if firsrR < secondR {
                //前面的cell 显示的范围多!
                section = emoiconCollectionView.indexPathForCell(firstCell)!.section
                cell = firstCell
            
            }else {
                section = emoiconCollectionView.indexPathForCell(secondCell)!.section
                cell = secondCell
            }
            //通过cell获取对应的indexPath
            let indexPath = emoiconCollectionView.indexPathForCell(cell)!
//            print(firsrR,secondR)
            toolBar.selectButtonWithSection(section)
            //设置pagecontrol滚动
            setPageControlDataForIndexPath(indexPath)
        }
        
        
        
    }
    
    
    
    
    
    
    
    
}

